from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


class BasePage(object):
    def __init__(self):
        try:
            self.driver = webdriver.Chrome()
        except Exception:
            raise NameError("Not Chrome")
        self.url = "http://cn.bing.com"

    # 打开页面
    def open(self, url):
        if url != "":
            self.driver.get(url)
            self.driver.maximize_window()
        else:
            raise ValueError("not value")

    # 定位元素 *解包方法将多个参数值传入location变量以实现传递多个参数值
    def find_element(self, location, driver):
        self.driver = driver
        try:
            # global elem
            type = location[0]
            value = location[1]
            if type == "id" or type == "ID" or type == "Id":
                elem = self.driver.find_element_by_id(value)
                loc_by = (By.ID, value)
            elif type == "name" or type == "NAME" or type == "Name":
                elem = self.driver.find_element_by_name(value)
                loc_by = (By.NAME, value)
            elif type == "class" or type == "CLASS" or type == "Class" or type == "ClassName":
                elem = self.driver.find_element_by_class_name(value)
                loc_by = (By.CLASS_NAME, value)
            elif type == "link" or type == "LINK" or type == "LINK" or type == "LINK_TEXT":
                elem = self.driver.find_element_by_link_text(value)
                loc_by = (By.LINK_TEXT, value)
            elif type == "xpath" or type == "XPATH":
                elem = self.driver.find_element_by_xpath(value)
                loc_by = (By.XPATH, value)
            elif type == "css" or type == "CSS" or type == "Css" or type == "css_selector" or type == "CSS_SELECTOR":
                elem = self.driver.find_element_by_css_selector(value)
                loc_by = (By.CSS_SELECTOR, value)
                print(type)
            # return elem
            WebDriverWait(self.driver, 10, 0.5).until(EC.visibility_of_element_located(loc_by))
            return self.driver.find_element(*loc_by)
        except:
            print("元素未找到")

    # 输入内容
    def input(self, location, value, driver, clear_first=True, click_first=True):
        try:
            if click_first:
                # self.find_element(*location).click()
                self.find_element(location, driver).click()
            if clear_first:
                # self.find_element(*location).clear()
                self.find_element(location, driver).clear()
            # self.find_element(*location).send_keys(value)
            self.find_element(location, driver).send_keys(value)
        except AttributeError:
            print("%s 页面没有找到 %s 元素" % (self, location))

    def script(self, src):
        self.driver.execute_script(src)

    # 退出浏览器
    def quit(self):
        self.driver.quit()






